import os
import json

def merge_json_files(input_dir='output2', output_file='xiaoyu.json'):
    merged_data = []

    # 遍历文件夹中的所有文件
    for filename in sorted(os.listdir(input_dir)):
        if filename.endswith('.json'):
            file_path = os.path.join(input_dir, filename)
            print(f"Processing file: {file_path}")
            
            # 读取并加载每个 JSON 文件
            with open(file_path, 'r', encoding='utf-8') as file:
                try:
                    data = json.load(file)
                    if isinstance(data, list):  # 确保文件内容是列表
                        merged_data.extend(data)  # 合并数据
                    else:
                        print(f"Warning: {file_path} does not contain a list of JSON objects.")
                except json.JSONDecodeError as e:
                    print(f"Error decoding {file_path}: {e}")
    # 写入 JSONL 文件
    with open(output_file, "w", encoding="utf-8") as f:
        for item in merged_data:
            f.write(json.dumps(item, ensure_ascii=False) + "\n")

    print(f"✅ 转换完成，共处理 {len(merged_data)} 条数据，已保存为 {output_file}]")

# 调用函数合并文件
merge_json_files(input_dir='output2', output_file='xiaoyu.jsonl')



